var inival = 0; // Initialise starting element number
var groupnr = 0;

function addLinks() {
	var newArea = add_New_Element();
	var htcontents = "<td colspan='4'><center><a onclick='addTextBox(); addAutocomplete()' href='#'>Adauga Ingredient </a> "
			+ "&nbsp;&nbsp;|&nbsp;&nbsp; <a onclick='removeElement(); removeAutoComplete()' href='#'>Sterge Ingredient</a><center></td>";
	document.getElementById(newArea).innerHTML = htcontents;
}

function removeAll(){
	var d = document.getElementById('area');
	while(inival != 0){
		var olddiv = document.getElementById("my" + inival + "Div");
		d.removeChild(olddiv);
		inival--;
	}
}
// Call this function to add a textarea
function addTextArea() {
	var newArea = add_New_Element_area();
	var htcontents = "<td colspan='2'><center><textarea name='observatii' cols=70 rows=3>Observatii</textarea></center></td>";
	document.getElementById(newArea).innerHTML = htcontents;
}

function add_New_Element_area() {	
	var ni = document.getElementById('areatextarea');
	var newdiv = document.createElement('tr'); // Create dynamic element
	var textIdName = 'myTextArea';
	newdiv.setAttribute('id', textIdName);
	ni.appendChild(newdiv);
	return textIdName;
}
function removeElementTextArea() {
	var d = document.getElementById('areatextarea');
	var olddiv = document.getElementById("myTextArea");
	d.removeChild(olddiv);
}

// Call this function to add textbox with params
function addTextBox1(value1, value2) {
	var newArea = add_New_Element();
	groupnr += 1;
	var index = 1;
	var htcontents = "<tr><td>Nume Produs: </td><td><input type='text' name='"
			+ groupnr
			+ "_"
			+ index++
			+ "' required='required' id='autocompleteinput"
			+ groupnr
			+ "' value="
			+ value1
			+ "/></td>"
			+ "<td>Valoare: </td><td><input style='width: 30px' type='text' name='"
			+ groupnr + "_" + index++ + "' required='required' value=" + value2
			+ "/></td></tr>";

	document.getElementById(newArea).innerHTML = htcontents;

}
// Call this function to add textbox
function addTextBox() {
	var newArea = add_New_Element();
	groupnr += 1;
	var index = 1;
	var htcontents = "<tr><td>Nume Produs: </td><td><input type='text' name='"
			+ groupnr
			+ "_"
			+ index++
			+ "' required='required' id='autocompleteinput"
			+ groupnr
			+ "'/></td>"
			+ "<td>Valoare: </td><td><input style='width: 30px' type='text' name='"
			+ groupnr + "_" + index++ + "' required='required'/></td></tr>";

	document.getElementById(newArea).innerHTML = htcontents;

}
function add_New_Element() {
	inival = inival + 1; // Increment element number by 1
	var ni = document.getElementById('area');
	var newdiv = document.createElement('tr'); // Create dynamic element
	var divIdName = 'my' + inival + 'Div';
	newdiv.setAttribute('id', divIdName);
	ni.appendChild(newdiv);
	return divIdName;
}

function removeElement() {
	var d = document.getElementById('area');
	var olddiv = document.getElementById("my" + inival + "Div");
	d.removeChild(olddiv);
	if (inival != 0) {
		inival--;
	}
	if (groupnr != 0) {
		groupnr--;
	}

}
